package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxwzxy.medic.entity.*;
import com.gxwzxy.medic.service.LoginService;
import com.gxwzxy.medic.service.MenuService;
import com.gxwzxy.medic.service.Impl.LoginServiceImpl;
import com.gxwzxy.medic.service.Impl.MenuServiceImpl;
import com.my.web.servlet.RequestBeanUtils;

/**
 * Servlet implementation class login
 */
public class LoginServlet extends  BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @throws IOException 
     * @see HttpServlet#HttpServlet()
     */

     public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException{


    	//鑾峰彇鍝嶅簲鏁版嵁
    	User user = RequestBeanUtils.requestToBean(req, User.class); 

 		//根据userName,Passowrd查找
 		LoginService ls = new LoginServiceImpl();
 		List<User> lu = ls.userLogin(user.getUserName() ,user.getPassword());
		if(lu!=null) {
			MenuService ms = new MenuServiceImpl();
			//查询全部的额menu
			List<Menu> lm = ms.findMenu();
			//根据userId查询menuId
			List<Menu> mid = ms.findMenuByUserId(lu.get(0).getUserId());
//			for(int i = 0;i<mid.size();i++) {
//				System.out.println(mid.get(i).getMenuId());
//			}
			//鍒涘缓session瀵硅薄
 	 		HttpSession session = req.getSession();
 	 		session.setAttribute("user", lu);//登录的个人账号
 	 		session.setAttribute("listm", lm);//全部的菜单信息
 	 		session.setAttribute("mid", mid);//该账号下的角色菜单信息
 	 	    //跳转主界面
 	 		//resp.sendRedirect("main.jsp");
 	 		resp.getWriter().write("main.jsp");
 		}else {
 	 		resp.getWriter().write("false");
		}
      }
}
